package com.zhoug.router3;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.template.IProvider;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zhoug.logger3.Logger;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

/**
 * 路由管理
 *
 * @Author: zhoug
 * @Date: 2025-01-21
 * @Description:
 */
public class Router {
    private static final String TAG = ">>>Router";

    private volatile static boolean debug = false;
    private static Application application;

    public static void init(Application application, boolean debug) {
        Router.application = application;
        Router.debug = debug;
        //必须在init之前
        if (debug) {
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(application);
    }

    protected static boolean isDebug() {
        return Router.debug;
    }


    protected static Application getApplication() {
        return application;
    }


    public static ARouter getARouter() {
        return ARouter.getInstance();
    }

    /**
     *  获取服务 必须实现接口 {@link IProvider}
     * @param path
     * @param clz
     * @return
     * @param <T>
     * @noinspection unchecked
     */
    public static <T> T getProvider(String path, Class<T> clz) {
        Object object = Router.getARouter().build(path).navigation();
        if (object != null) {
            if (clz.isAssignableFrom(object.getClass())) {
                return (T) object;
            }
        }
        return null;
    }

    /**
     *  获取服务 必须实现接口 {@link IProvider}
     * @param clz
     * @return
     * @param <T>
     */
    public static <T> T getProvider(Class<? extends T> clz) {
        return Router.getARouter().navigation(clz);
    }

    /**
     * 根据path创建新的Fragment
     *
     * @param path
     * @param bundle args
     * @return
     */
    public static Fragment createNewFragment(String path, Bundle bundle) {
        Object obj = getARouter().build(path).with(bundle).navigation();
        if (obj instanceof Fragment fragment) {
            return fragment;
        } else {
            return null;
        }
    }

    /**
     * 根据path创建新的Fragment
     *
     * @param path
     * @return
     */
    public static Fragment createNewFragment(String path) {
        return createNewFragment(path, null);
    }

    /**
     * 发现或者创建新的Fragment
     *
     * @param fragmentManager
     * @param path            路由,Tag
     * @return
     */
    public static Fragment findOrCreateByPath(FragmentManager fragmentManager, String path) {
        if (path == null) {
            return null;
        }
        Fragment fragment = fragmentManager.findFragmentByTag(path);
        if (fragment == null) {
            fragment = createNewFragment(path);
            if (isDebug())
                Logger.d(TAG, "创建新的:" + fragment);
        }
        return fragment;
    }


    /**
     * 发现或者创建新的Fragment
     *
     * @param fm
     * @param cls
     * @param <T>
     * @return
     */
    public static <T extends Fragment> T findOrCreateByTag(FragmentManager fm, Class<T> cls) {
        return findOrCreateByTag(fm, cls, null);
    }

    /**
     * 发现或者创建新的Fragment
     *
     * @param fm
     * @param cls
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends Fragment> T findOrCreateByTag(FragmentManager fm, Class<T> cls, String tag) {
        if (tag == null) {
            tag = cls.getCanonicalName();
        }
        T fragmentByTag = (T) fm.findFragmentByTag(tag);
        if (fragmentByTag == null) {
            try {
                fragmentByTag = cls.newInstance();
                if (isDebug()) {
                    Logger.d(TAG, "findOrCreateByTag: 创建新的" + cls.getName());
                }
            } catch (IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        } else {
            if (isDebug()) {
                Logger.d(TAG, "findOrCreateByTag: 复用:" + fragmentByTag);

            }
        }
        return fragmentByTag;
    }

    /**
     * 启动activity
     *
     * @param context
     * @param cls
     */
    public static void navigation(Context context, Class<? extends Activity> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }


    /**
     * 启动activity
     *
     * @param context
     * @param cls
     * @param bundle
     */
    public static void navigation(Context context, Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    /**
     * startActivityForResult
     *
     * @param activity
     * @param cls
     * @param bundle
     * @param requestCode
     */
    @Deprecated
    public static void navigation(Activity activity, Class<? extends Activity> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * startActivityForResult
     *
     * @param fragment
     * @param cls
     * @param bundle
     * @param requestCode
     */
    @Deprecated
    public static void navigation(Fragment fragment, Class<? extends Activity> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(fragment.getContext(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        fragment.startActivityForResult(intent, requestCode);
    }


}
